﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
using YouMa.Model;

namespace YouMa
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public static IContainer ApplicationContainer { get; private set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            string contectstr = Configuration.GetConnectionString("SqlServerConnection");
            services.AddDbContext<YouMaContext>(option => option.UseSqlServer(contectstr));


            //注册swagger
            services.AddSwaggerGen(c =>
            {
                c.DescribeAllParametersInCamelCase();
                c.SwaggerDoc("v1", new Info { Title = "YouMa接口管理平台", Version = "v1" });
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                //var xmlPath = Path.Combine(basePath, xmlname+".xml");
                foreach (var name in Directory.GetFiles(basePath, "*.XML", SearchOption.AllDirectories))
                {
                    c.IncludeXmlComments(name);
                }
                // c.OperationFilter<AddAuthTokenHeaderParameter>();
            });
            services.AddMvcCore().AddApiExplorer();//添加api文件

            services.AddMvc();

            //根据autofac注入
            var builder = new ContainerBuilder();//实例化 AutoFac  容器      
            builder.RegisterAssemblyTypes(System.Reflection.Assembly.Load("YouMa" + ".Service"));
            builder.RegisterAssemblyTypes(System.Reflection.Assembly.Load("YouMa" + ".Reposity"));
            builder.Populate(services);
            ApplicationContainer = builder.Build();
            return new AutofacServiceProvider(ApplicationContainer);//第三方IOC接管 core内置DI容器  
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.ShowExtensions();
                c.DisplayRequestDuration();
                // c.ValidatorUrl(null);
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "接口");
            });
            app.UseMvc();
        }
    }
}
